package com.dkd.manage.service.impl;

import java.util.Collections;
import java.util.List;
import com.dkd.common.utils.DateUtils;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;

/**
 * 设备Service业务层处理
 * 
 * @author fshh
 * @date 2025-04-01
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;


    @Autowired
    private VmTypeMapper vmTypeMapper;



    @Autowired
    private NodeMapper nodeMapper;



    /**
     * 查询设备
     * 
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备列表
     * 
     * @param vendingMachine 设备
     * @return 设备
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备
     * 
     * @param vendingMachine 设备
     * @return 结果
     */
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        vendingMachine.setCreateTime(DateUtils.getNowDate());

        String name = vendingMachine.getAddr();
        StringBuilder sb = new StringBuilder();

        // 将每个汉字转换为拼音，并取首字母
        for (char c : name.toCharArray()) {
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
            if (pinyinArray != null && pinyinArray.length > 0) {
                sb.append(pinyinArray[0].charAt(0));
            } else {
                // 无法转换的字符（如非汉字字符），直接添加原字符
                sb.append(c);
            }
        }


//提取传过来的类型id 获取最大容量
        int VmTypeId = Math.toIntExact(vendingMachine.getVmTypeId());
        int maxCapacity =  vmTypeMapper.selectMaxCapacity( VmTypeId);
        vendingMachine.setChannelMaxCapacity(Long.valueOf(maxCapacity));

        int nodeId = Math.toIntExact(vendingMachine.getNodeId());
      int  regionId = nodeMapper.selectRegionId(nodeId);
        vendingMachine.setRegionId(Long.valueOf( regionId));

        vendingMachine.setPartnerId(Long.valueOf( nodeMapper.selectPartnerId(nodeId)));




       vendingMachine.setInnerCode(sb.toString()+vendingMachine.getNodeId()+vendingMachine.getVmTypeId());

        return vendingMachineMapper.insertVendingMachine(vendingMachine);
    }

    /**
     * 修改设备
     * 
     * @param vendingMachine 设备
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备
     * 
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备信息
     * 
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    @Override
    public List<VendingMachine> selectVendingMachineListbynode(int id) {
        return vendingMachineMapper.selectVendingMachineBynodeId(id);
    }
}
